package com.zhonghui.license;

import java.util.Scanner;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.function.Function;
import java.net.InetAddress;
import java.util.List;

public class WindowsServerInfos extends AbstractServerInfos
{
    @Override
    protected List<String> getIpAddress() throws Exception {
        List<String> result = null;
        final List<InetAddress> inetAddresses = this.getLocalAllInetAddress();
        if (inetAddresses != null && inetAddresses.size() > 0) {
            result = inetAddresses.stream().map(InetAddress::getHostAddress).distinct().map(String::toLowerCase).collect(Collectors.toList());
        }
        return result;
    }
    
    @Override
    protected List<String> getMacAddress() throws Exception {
        List<String> result = null;
        final List<InetAddress> inetAddresses = this.getLocalAllInetAddress();
        if (inetAddresses != null && inetAddresses.size() > 0) {
            result = inetAddresses.stream().map(this::getMacByInetAddress).distinct().collect(Collectors.toList());
        }
        return result;
    }
    
    @Override
    protected String getCPUSerial() throws Exception {
        String serialNumber = "";
        final Process process = Runtime.getRuntime().exec("wmic cpu get processorid");
        process.getOutputStream().close();
        final Scanner scanner = new Scanner(process.getInputStream());
        if (scanner != null && scanner.hasNext()) {
            scanner.next();
        }
        if (scanner.hasNext()) {
            serialNumber = scanner.next().trim();
        }
        scanner.close();
        return serialNumber;
    }
    
    @Override
    protected String getMainBoardSerial() throws Exception {
        String serialNumber = "";
        final Process process = Runtime.getRuntime().exec("wmic csproduct get UUID");
        process.getOutputStream().close();
        final Scanner scanner = new Scanner(process.getInputStream());
        if (scanner != null && scanner.hasNext()) {
            scanner.next();
        }
        if (scanner.hasNext()) {
            serialNumber = scanner.next().trim();
        }
        scanner.close();
        return serialNumber;
    }
}
